package com.eazy.lkv2.web.controller.activity;

import com.eazy.lkv2.core.Global;
import com.eazy.lkv2.core.util.PostMaker;
import com.eazy.lkv2.core.util.QiniuUpload;
import com.eazy.lkv2.web.entity.Activitys;
import com.eazy.lkv2.web.entity.ConferenceHall;
import com.eazy.lkv2.web.service.activity.ActivityService;
import com.eazy.lkv2.web.service.hotel.ConferenceHallService;
import com.eazy.lkv2.web.service.hotel.HotelService;
import com.eazy.lkv2.web.vo.ActivityPoster;
import com.eazy.lkv2.web.vo.ActivityVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.eazy.lkv2.web.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author hpb
 * @date 2016.9.8
 * @desc 活动后台管理
 */
@Controller("activitys")
@RequestMapping("/activity")
public class ActivityController extends BaseController<Activitys, Integer> {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private HotelService hotelService;


    @Autowired
    ConferenceHallService conferenceHallService;



    /**
     * 后台活动列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        List<Activitys>activitys=activityService.selectAll();
        List<ActivityVO>activityVOList=new ArrayList<>();
        for(Activitys activity:activitys){
            activityVOList.add(ActivityVO.get(activity));
        }
        model.addAttribute("data", activityVOList);
        return "activity/activity_list";
    }

    /**
     * 跳转到添加活动页面
     */
    @RequestMapping(value = "/toAddActivity", method = RequestMethod.GET)
    public ModelAndView toAddLine() {
        addObject("hotelData", hotelService.select());
        addObject("hallData",conferenceHallService.queryByObject(null));
        return getSuccess("activity/activity_add");
    }

    /**
     * 新增活动
     * @param activitys
     */
    @RequestMapping(value = "/addActivity", method = RequestMethod.POST)
    public void addActivity(Activitys activitys) {
        String[] images = getParaValues("image");
        String[] success = getParaValues("success");

        if (images == null) {
            renderJsonError("活动图片不能为空");
            return;
        }
        if (success == null) {
            renderJsonError("请先上传图片，然后在提交！！！！");
            return;
        }

        String photo=QiniuUpload.IMG_HOST+images[0];
        ConferenceHall conferenceHall=conferenceHallService.findById(activitys.getHallId().toString());
        ActivityPoster activityPoster=new ActivityPoster();
        activityPoster.setTitle(activitys.getTitle());
        activityPoster.setPoster(photo);
        SimpleDateFormat df=new SimpleDateFormat("YYYY-MM-dd HH:mm");
        activityPoster.setTime(df.format(activitys.getBeginTime()));
        activityPoster.setAddr(conferenceHall.getName());
        activitys.setActivitysImg(photo);
        Integer id=activityService.create(activitys);
        activityPoster.setQrcodeUrl(Global.getDomain()+"mobileActivity/event/"+id);
        String posterName=PostMaker.genPost(activityPoster);
        activitys.setPoster(Global.getDomain() + posterName);
        activityService.update(activitys);
        renderResult(id > 1);
    }

    /**
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "toUpdate/{id}", method = RequestMethod.GET)
    public String toUpdate(@PathVariable("id") String id,Model model) {
        model.addAttribute("hotelData", hotelService.select());
        Activitys activity = activityService.findById(Integer.parseInt(id));
        model.addAttribute("activity", ActivityVO.get(activity));
        return "activity/activity_update";
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void updateActivitys(Activitys activitys) {
        Activitys oldActivity = activityService.findById(activitys.getId());
        String[] images = getParaValues("image");

        if (images == null) {
            activitys.setActivitysImg(oldActivity.getActivitysImg());
        }else{
            String allPhoto = "";
            for(String img:images){
                allPhoto+=QiniuUpload.IMG_HOST+img+",";
            }

            activitys.setActivitysImg(allPhoto.substring(0, allPhoto.length() - 1));
        }
        int result=activityService.update(activitys);
        renderResult(result>0);
    }


    /**
     * 根据ID删除活动
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deletes/{ids}", method = RequestMethod.POST)
    @ResponseBody
    public String deleteActivity(@PathVariable("ids") String[] ids) {
        for (String id : ids) {
            activityService.removeById(Integer.parseInt(id));
        }
        return "success";
    }

    /**
     * 禁用活动
     * @param id
     * @return
     */
    @RequestMapping(value = "disable/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String disableActivity(@PathVariable("id") String id) {
        Activitys activity = activityService.findById(Integer.parseInt(id));
        if (activity != null && activity.isStatus()) {
            activity.setStatus(false);
        }
        activityService.update(activity);
        return "success";
    }

    /**
     * 启用活动
     * @param id
     * @return
     */
    @RequestMapping(value = "enable/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String enableActivity(@PathVariable("id") String id) {
        Activitys activity = activityService.findById(Integer.parseInt(id));
        if (activity != null && !activity.isStatus()) {
            activity.setStatus(true);
        }
        activityService.update(activity);
        return "success";
    }

    /**
     *
     * @param  id  activity id
     * @return
     */
//    @RequestMapping(value = "genPoster/{id}")
//    public ModelAndView generatePoster(@PathVariable("id") String id){
//        Activitys activity = activityService.findById(Integer.parseInt(id));
//        ActivityPoster activityPoster=new ActivityPoster();
//        activityPoster.setTitle(activity.getTitle());
//        activityPoster.setPoster(activity.getActivitysImg());
//        SimpleDateFormat df=new SimpleDateFormat("YYYY-MM-DD HH:mm");
//        activityPoster.setTime(df.format(activity.getBeginTime()));
////        activityPoster.setAddr(activity.);
//
//        new File(SystemConfig.getPosterSavePath()).mkdirs();
//        String name=getName(id);
//        PostMaker.genPost(activityPoster,SystemConfig.getPosterSavePath()+name);
//
//
//        return getSuccess("activity/poster_preview");
//    }
//    private String getName(String id){
//        return  ""+System.currentTimeMillis()+"_"+id+"_poster.jpg";
//    }
}
